package com.yohann.exam_service.controller;

import com.yohann.exam_service.entity.Reading;
import com.yohann.exam_service.entity.Writing;
import com.yohann.exam_service.service.ReadingService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 阅读表 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-11-19
 */
@Api(tags = "阅读")
@RestController
@RequestMapping("/exam_service/reading")
public class ReadingController {
    @Autowired
    private ReadingService readingService;

    /**
     * 添加阅读题目
     *
     * @param reading 题目信息
     * @return 添加状态
     */
    @ApiOperation("添加阅读题目")
    @PostMapping("addReading")
    public Result addReading(@ApiParam("题目信息") @RequestBody Reading reading) {
        reading.setCreateTime(new Date());
        boolean save = readingService.save(reading);

        return Result.state(save);
    }

    /**
     * 删除阅读题目
     *
     * @param id 题目id
     * @return 删除状态
     */
    @ApiOperation("删除阅读题目")
    @GetMapping("deleteReading/{id}")
    public Result deleteReading(@ApiParam("题目id") @PathVariable int id) {
        boolean remove = readingService.removeById(id);

        return Result.state(remove);
    }

    /**
     * 查询阅读题目
     *
     * @param type 题目类型
     * @return 题目集合
     */
    @ApiOperation("查询阅读题目")
    @GetMapping("queryReading/{type}")
    public Result queryReading(@ApiParam("题目类型") @PathVariable int type) {
        List<Reading> list = readingService.queryReading(type);

        return Result.ok().data("list", list);
    }

    /**
     * 分页查询阅读题目
     *
     * @param current 起始位置
     * @param limit   结束位置
     * @param reading 题目分类
     * @return 结果集
     */
    @ApiOperation("分页查询阅读题目")
    @PostMapping("query/{current}/{limit}")
    public Result queryReading(@ApiParam("起始位置") @PathVariable int current,
                               @ApiParam("结束位置") @PathVariable int limit,
                               @ApiParam("查询条件") @RequestBody(required = false) Reading reading) {
        Map<String, Object> map = readingService.queryWriting(current, limit, Objects.isNull(reading) ? 1 : reading.getType());

        return Result.ok().data(map);
    }

    /**
     * 通过id获取阅读题目信息
     *
     * @param id 题目id
     * @return 题目信息
     */
    @ApiOperation("通过id获取阅读题目信息")
    @GetMapping("getReadingById/{id}")
    public Result getReadingById(@ApiParam("题目id") @PathVariable int id) {
        Reading reading = readingService.getById(id);

        return Result.ok().data("item", reading);
    }

    /**
     * 修改阅读题目
     *
     * @param reading 题目信息
     * @return 修改状态
     */
    @ApiOperation("修改阅读题目")
    @PostMapping("updateReading")
    public Result updateReading(@ApiParam("题目信息") @RequestBody Reading reading) {
        boolean update = readingService.updateById(reading);

        return Result.state(update);
    }
}

